package cn.lau.common.security;

import cn.lau.auth.entity.SysUser;
import cn.lau.auth.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        System.out.println("loadUserByUsername()");
        //通过用户名查询出对应的系统用户
        SysUser sysUser=sysUserService.findByName(username);
        if(sysUser==null){
            throw  new UsernameNotFoundException("没找到账号");
        }
        SecurityUser securityUser=new SecurityUser(sysUser);
        return securityUser;
    }
}
